
package com.liy.framework.web.spring.utils;

import com.liy.framework.common.utils.ContextHandler;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class RestTemplateUtil {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${dascloud.bizsys.sys:''}")
    private String sysServerUrl;

    @Value("${dascloud.bizsys.sso:''}")
    private String ssoServerUrl;

    @Value("${dascloud.bizsys.jk:''}")
    private String jkServerUrl;

    @Value("${dascloud.bizsys.base:''}")
    private String baseServerUrl;

    @Value("${dascloud.bizsys.pw:''}")
    private String pwServerUrl;

    @Value("${dascloud.bizsys.yy:''}")
    private String yyServerUrl;

    @Value("${dascloud.bizsys.msg:''}")
    private String msgServerUrl;

    public RestTemplateUtil() {
    }


    /**
     *
     */
    public <T> T get(String url, Class<T> tClass) {
        return (T) this.get(url, null, tClass);
    }

    /**
     *
     */
    public <T> T get(String url, Map<String, Object> params, Class<T> tClass) {
        if (params == null) {
            params = new HashMap();
        }

        // 拼接的 token 信息
        StringBuffer paramsBuffer = new StringBuffer("?JwtToken={JwtToken}");


        // keySet
        Iterator<String> iterator = params.keySet().iterator();

        //
        while(iterator.hasNext()) {
            String key = iterator.next();



            paramsBuffer.append("&" + key + "={" + key + "}");
        }

        params.put("JwtToken", ContextHandler.getJwtToken());

        // restTemplate 发送Get请求
        return this.restTemplate.getForObject(url + paramsBuffer,   tClass,  params);
    }

    public <T> T post(String url, Class<T> tClass) {
        return this.post(url, (Object)null, tClass);
    }

    public <T> T post(String url, Object params, Class<T> tClass) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("JwtToken", ContextHandler.getJwtToken());
        HttpEntity<Object> httpEntity = new HttpEntity(params, headers);
        return this.restTemplate.postForObject(url, httpEntity, tClass, new Object[0]);
    }


    public String getSysServerUrl() {
        return this.sysServerUrl;
    }

    public String getSsoServerUrl() {
        return this.ssoServerUrl;
    }

    public String getJkServerUrl() {
        return this.jkServerUrl;
    }

    public String getBaseServerUrl() {
        return this.baseServerUrl;
    }

    public String getPwServerUrl() {
        return this.pwServerUrl;
    }

    public String getYyServerUrl() {
        return this.yyServerUrl;
    }

    public String getMsgServerUrl() {
        return this.msgServerUrl;
    }
}
